package com.nt.train.service;

import com.nt.train.entity.Goods;

/**
 * @author hlf
 * @version 1.0
 * @date 2022/8/16 17:14
 */
public class GoodsService {

    GoodsCache goodsCache = GoodsCache.getGoodsCache();

    public Goods getById(String id){
        if(goodsCache.getCache(id) == null){
            Goods goods = GoodsBase.getBase().get(id);
            goodsCache.setCache(goods);
            System.out.println("从数据库读取数据");
            System.out.println(goods.getName());
            return goods;
        }
        System.out.println(goodsCache.getCache(id).getName());
        return goodsCache.getCache(id);
    }

    public void add(Goods goods){
        goodsCache.setCache(goods);
        GoodsBase.getBase().put(goods.getId(), goods);
        System.out.println("添加数据到数据库");
    }

    public void deleteById(String id){
        if(goodsCache.getCache(id) != null){
            goodsCache.delCache(id);
        }
        GoodsBase.getBase().remove(id);
    }
}
